package com.uyayo.system.monitor.service.impl;

import java.util.ArrayList;
import java.util.List;


import com.uyayo.system.monitor.entity.SysSession;
import com.uyayo.system.monitor.mapper.SysSessionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.framework.common.exception.CustomException;
import com.uyayo.framework.message.model.Result;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import com.uyayo.system.monitor.entity.SysEvent;
import com.uyayo.system.monitor.model.SysSessionCondition;
import com.uyayo.system.monitor.service.SessionsService;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service("sessionsService")
public class SessionsServiceImpl implements SessionsService {

	private final SysSessionMapper sessionMapper;

	@Autowired
	public SessionsServiceImpl(SysSessionMapper sessionMapper) {
		this.sessionMapper = sessionMapper;
	}

	@Override
	public SysSession getSessionById(String id) throws Exception{
		SysSession sysSession =sessionMapper.selectByPrimaryKey(id);
		if(sysSession==null){
			throw new CustomException("该用户会话不存在！");
		}
		return sysSession;
	}

	@Override
	public  PageBean<SysSession> selectSession(int pageNum,Integer pageSize,String sort,String order,SysSessionCondition sysSessionCondition) throws Exception {
		List<SysSession> sessions;

		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("startTime", "start_time"));
		orderBies.add(new OrderBy("endTime", "end_time"));
		orderBies.add(new OrderBy("created", "created"));

		OrderByUtil orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageNum, pageSize);
		Example example = new Example(SysSession.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(sysSessionCondition.getUsername())){
			criteria.andLike("username","%"+sysSessionCondition.getUsername()+"%");
		}
		if(sysSessionCondition.getBeginCreatedTime()!=null || sysSessionCondition.getEndCreatedTime()!=null){
			criteria.andBetween("createdTime", sysSessionCondition.getBeginCreatedTime(), sysSessionCondition.getEndCreatedTime());
		}
		sessions = sessionMapper.selectByExample(example);
		
		//取记录总条数
		PageInfo<SysSession> pageInfo = new PageInfo<>(sessions);
			
		return new PageBean<>(pageInfo.getTotal(), pageNum, pageSize, sessions);
	}
	
	@Override
	public Result deleteSession(String ids) {
		Result customResult;
		int countDelete = 0;
		int countDelete_ = 0;
		if (!StringUtils.isEmpty(ids)) {
			String[] idArr = ids.split(",");
			for (String anIdArr : idArr) {
				if (!anIdArr.equals("")) {
					sessionMapper.deleteByPrimaryKey(anIdArr);
					countDelete++;
				} else {
					countDelete_++;
				}
			}
			customResult = Result.build(200,"成功删除"+countDelete+"条记录，有"+countDelete_+"条记录删除失败！");
		}else{
			customResult = Result.build(400,"请选择要删除的记录！");
		}
		return customResult;
	}
	
	
	@Override
	public int deleteSessionById(String id) {
		return sessionMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int updateByLogoutTimeIsNull(SysSession sessions) {
		return sessionMapper.updateByLogoutTimeIsNull(sessions);
	}

	@Override
	public int insertSelective(SysSession sessions) {
		return sessionMapper.insertSelective(sessions);
	}

	@Override
	public int updateByPrimaryKeySelective(SysSession sessions) {
		return sessionMapper.updateByPrimaryKeySelective(sessions);
	}


	@Override
	public int deleteSessionMonitor(String ids) {
		if (!StringUtils.isEmpty(ids)) {
			String[] idArr = ids.split(",");
			for (int i = 0; i < idArr.length; i++) {
				if (!idArr[i].equals("")) {
					sessionMapper.deleteByPrimaryKey(idArr[i]);
				}
			}
			return 1;
		}else{
			return 0;
		}
	}

	@Override
	public List<SysSession> selectSessionMonitor(String sort,String order,SysSessionCondition sysSessionCondition) {

		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("startTime", "start_time"));
		orderBies.add(new OrderBy("endTime", "end_time"));
		orderBies.add(new OrderBy("created", "created"));

		OrderByUtil orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		Example example = new Example(SysEvent.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}

		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(sysSessionCondition.getUsername())){
			criteria.andLike("username","%"+sysSessionCondition.getUsername()+"%");
		}
		if(sysSessionCondition.getBeginCreatedTime()!=null || sysSessionCondition.getEndCreatedTime()!=null){
			criteria.andBetween("startTime", sysSessionCondition.getBeginCreatedTime(), sysSessionCondition.getEndCreatedTime());
		}
		return sessionMapper.selectByExample(example);

	}


}
